#include "HelloWorldScene.h"
#include "GameOverScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;

HelloWorld::~HelloWorld()
{
	if (_targets)
	{
		_targets->release();
		_targets = NULL;
	}
	if (_projectiles)
	{
		_projectiles->release();
		_projectiles = NULL;
	}
	// cpp don't need to call super dealloc
	// virtual destructor will do this
}

HelloWorld::HelloWorld()
			:_targets(NULL)
			,_projectiles(NULL)
{
	_projectilesDestroyed = 0;
}
	


CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

//the addTarget()
void HelloWorld::addTarget()
{
	CCSprite *target = CCSprite::create("Target.png",CCRectMake(0,0,27,40));

	//Determine where to spawn the target along the Y axis
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	int minY = target->getContentSize().height/2;
	int maxY = winSize.height - target->getContentSize().height/2;
	int rangeY = maxY - minY;

	//srand(TimGetTicks())
	int actualY = (rand() % rangeY) + minY;

	//Create the target slightly off-screen along the right edge,
	//and along a random position along the Y axis as calculated
	target->setPosition(ccp(winSize.width + (target->getContentSize().width / 2),actualY));

	this->addChild(target);

	int minDuration = (int)2.0;
	int maxDuration = (int)4.0;
	int rangeDuration = maxDuration - minDuration;
	//srand(TimGetTicks())
	int actualDuration = (rand() % rangeDuration) + minDuration;

	//Create the actions
	CCFiniteTimeAction* actionMove = CCMoveTo::create( (float)actualDuration, ccp(0 - target->getContentSize().width / 2 , actualY));
	CCFiniteTimeAction* actionMoveDone = CCCallFuncN::create( this, callfuncN_selector(HelloWorld::spriteMoveFinished));
	target->runAction( CCSequence::create(actionMove,actionMoveDone,NULL));

	this->setTouchEnabled(true);

	target->setTag(1);
	_targets->addObject(target);
}

//method of ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
	//Choose one of the touches to work with
	CCTouch* touch = (CCTouch*)(touches->anyObject());
	CCPoint location = touch->getLocationInView();
	location = CCDirector::sharedDirector()->convertToGL(location);

	//Set up initial location of projectile
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	CCSprite *projectile = CCSprite::create("Projectile.png",CCRectMake(0,0,20,20));
	projectile->setPosition( ccp(20, winSize.height / 2));

	//Determinie ofset of location to projectile
	int offX = location.x - projectile->getPosition().x;
	int offY = location.y - projectile->getPosition().y;

	//Bail out if we are shooting down or backwards
	if (offX <= 0) return;

	//Ok to add now -we've double checked position
	this->addChild(projectile);

	//Determine where we wish to shoot the projectile to 
	int realX = winSize.width + (projectile->getContentSize().width / 2);
	float ratio = (float)offY / (float)offX;
	int realY = (realX * ratio) + projectile->getPosition().y;
	CCPoint realDest = ccp(realX, realY);

	//Determine the length of how far we're shooting
	int offRealX = realX - projectile->getPosition().x;
	int offRealY = realY - projectile->getPosition().y;
	float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
	float velocity = 480/1; //480pixels/1sec
	float realMoveDuration = length/velocity;

	//move projectile to actual endpoint
	projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration,realDest),
		CCCallFuncN::create(this,callfuncN_selector(HelloWorld::spriteMoveFinished)),
		NULL));

	projectile->setTag(2);
	_projectiles->addObject(projectile);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
}


//method spriteMoveFinished()
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
	CCSprite *sprite = (CCSprite *)sender;
	this->removeChild( sprite,true);

	if (sprite->getTag() == 1)
	{
		//target
		_targets->removeObject(sprite);

		GameOverScene *gameOverScene = GameOverScene::create();
		gameOverScene->getLayer()->getLabel()->setString("You Lose!");
		CCDirector::sharedDirector()->replaceScene(gameOverScene);
	}
	else if(sprite->getTag() == 2)
	{
		//projectile
		_projectiles->removeObject(sprite);

	}
}


//method update(float dt) update the scene
void HelloWorld::update(float dt)
{
	CCArray *projectilesToDelete = new CCArray;
	CCArray* targetsToDelete = new CCArray;
	CCObject* it = NULL;
	CCObject* jt = NULL;
		
	CCARRAY_FOREACH(_projectiles,it)
	{
		CCSprite *projectile = dynamic_cast<CCSprite*>(it);
		CCRect projectileRect = CCRectMake(
			projectile->getPosition().x - (projectile->getContentSize().width / 2),
			projectile->getPosition().y - (projectile->getContentSize().height /2 ),
			projectile->getContentSize().width,
			projectile->getContentSize().height);
		CCARRAY_FOREACH(_targets,jt)
		{
			CCSprite *target = dynamic_cast<CCSprite*>(jt);
			CCRect targetRect = CCRectMake(
				target->getPosition().x - (target->getContentSize().width / 2),
				target->getPosition().y - (target->getContentSize().height /2 ),
				target->getContentSize().width,
				target->getContentSize().height);

			if(projectileRect.intersectsRect(targetRect))
			{
				targetsToDelete->addObject(target);
				projectilesToDelete->addObject(projectile);
			}
		}
	}

	CCARRAY_FOREACH(targetsToDelete,jt)
	{
		CCSprite *target = dynamic_cast<CCSprite*>(jt);
		_targets->removeObject(target);
		this->removeChild(target,true);
	}

	CCARRAY_FOREACH(projectilesToDelete,it)
	{
		CCSprite* projectile = dynamic_cast<CCSprite*>(it);
		_projectiles->removeObject(projectile);
		this->removeChild(projectile,true);
		//Count
		_projectilesDestroyed++;
		if(_projectilesDestroyed > 30)
		{
			GameOverScene *gameOverScene = GameOverScene::create();
			gameOverScene->getLayer()->getLabel()->setString("You win!");
			CCDirector::sharedDirector()->replaceScene(gameOverScene);

		}
	}

	projectilesToDelete->release();
	targetsToDelete->release();

}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{	
	_targets = new CCArray;
	_projectiles = new CCArray;

    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());
		if( !CCLayerColor::initWithColor( ccc4(255,255,255,255)));


		//add the audio file
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-acc.wav",true);


        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////

		        // Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage* pCloseItem = CCMenuItemImage::create(
            "Closenormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);


		// Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();

		// Add the Player.png
		CCSprite* player = CCSprite::create("Player.png",CCRectMake(0,0,27,40));
        CC_BREAK_IF(! player);


		
        // Place the sprite on the center of the screen
		player->setPosition(ccp(player->getContentSize().width / 2, size.height / 2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(player ,1);

		// Add Target.png

		//add png
		CCSprite* target = CCSprite::create("Target.png",CCRectMake(0,0,30,50));
		CC_BREAK_IF(! target);

		target->setPosition(ccp(size.width/2,size.height/2));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(target, 1);

        // Add Projectile.png

		CCSprite* projectile = CCSprite::create("Projectile.png",CCRectMake(0,0,20,20));
		CC_BREAK_IF(! projectile);

		projectile->setPosition(ccp(size.width / 2 - 50,size.height / 2 - 50));

		this->addChild(projectile,1);

		//Call game logic about every second
		this->schedule(schedule_selector(HelloWorld::gameLogic),1.0);
		this->schedule( schedule_selector(HelloWorld::update));

        bRet = true;
    } while (0);
	
	

    return bRet;
}

//method gameLogic()
void HelloWorld::gameLogic(float dt)
{
	this->addTarget();
}

//menuCloseCallback,close btn

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

